import { ElMessage } from "element-plus";
import { getRolesByIdApi } from "../../api/role";
import { useStore } from "vuex";

const store = useStore();

const state = {
  roleList: []
};

const getters = {
  roleList: (state) => state.roleList
};
const mutations = {
  setRoleList (state, roleList) {
    console.log("setRoleList2", roleList);
    state.roleList = roleList;
  }
};
const actions = {
  async getRoles ({ commit, state, rootGetters }) {
    const { data: roleList } = await getRolesByIdApi(rootGetters["user/userId"]);
    console.log('getRoles:',roleList);
    if (!roleList) {
      ElMessage.error("当前用户下没有创建的角色...");
      return false;
    }
    if (Array.isArray(roleList)) {
      commit("setRoleList", roleList);
      return roleList;
    } else {
      ElMessage.error("角色信息接口异常");
      return false;
    }
  }

};
export default { state, getters, mutations, actions };
